<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>itbyteset</title>
		<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="js/browserMqtt.js"></script>
		<script type="text/javascript" src="js/itbyteset.js"></script>
	</head>

	<body id="body">
		<div rtdata="level1/aa/tag1_num">itbyteset测试1</div>
		<div rtdata="level1/aa/tag2_num">itbyteset测试2</div>
		<script>
			var url = "ws:sealush.tpddns.cn:8083/mqtt";
			var sun = 0;
			var clientId = "example2";
			var user = "";
			var password = "";
			var dest = "level1/+/+";
			var itbytes = new Itbytes();
			var onConnect = function(frame) { //mqtt连接成功回调
				//成功后需要订阅主题
				client.subscribe(dest);
				console.log("mqtt connect to [%s] success", dest)
			};
			var onConnectionLost = function() { //mqtt连接丢失回调
				console.log("mqtt [%s] disconnect", dest)
			};
			var onFailure = function() { //mqtt连接失败回调
				console.log("mqtt connect to [%s] fail", dest)
			};
			var client = mqtt.connect(url, {
				username: user,
				password: password,
				protocol: "ws"
			})
			client.on('connect', onConnect);
			client.on('close', onConnectionLost);
			client.on('error', onFailure);
			client.on("message", itbytes.callBack)

			itbytes.autodom(true);
			console.log(itbytes.getAllTag());
			$("#body").append("<div rtdata = 'level1/aa/tag3_num' > itbyteset测试3 </div>");
			itbytes.DOMchanged();
			console.log(itbytes.getAllTag());

			function doSomeThingTag0(rtdata) {
				itbytes.usetag(doSomeThingTag0, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			function doSomeThingTag1(rtdata) {
				itbytes.usetag(doSomeThingTag1, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			function doSomeThingTag2(rtdata) {
				itbytes.usetag(doSomeThingTag2, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			doSomeThingTag0();
			doSomeThingTag1();
			doSomeThingTag2();
		</script>
	</body>

</html>